Imports KanBan.Pgd.Infrastructure.Exceptions
Imports NHibernate.Criterion
Imports KanBan.Pgd.Domain
Imports KanBan.Pgd.Domain.Entities
Imports NHibernate

Namespace Repositories

    Public Class PerformanceEvaluationRepository
        Inherits RepositoryBase(Of PerformanceEvaluation)

        Public Function GetAllByEvaluator(ByVal evaluatorId As Guid) As IList(Of PerformanceEvaluation)

            Try

                Dim session As ISession = NHibernateHelper.GetCurrentSession()
                Dim entities As IList(Of PerformanceEvaluation) = New List(Of PerformanceEvaluation)

                entities = session _
                    .CreateCriteria(GetType(PerformanceEvaluation)) _
                    .CreateAlias("Evaluator", "e") _
                    .Add(Restrictions.Eq("e.Id", evaluatorId)) _
                    .Add(Restrictions.Eq("IsDeleted", False)) _
                    .List(Of PerformanceEvaluation)()

                Return entities

            Catch ex As Exception

                Throw New TechnicalException(String.Format("Error while trying to 'GetAllByEvaluator' entities of type = {0} with evaluatorId = {1} - Details: {2}", _
                                            GetType(PerformanceEvaluation).ToString(), evaluatorId.ToString(), ex.Message), ex.InnerException)

            End Try

        End Function

        'Public Sub SaveOrUpdateGoalRating(ByVal pe As PerformanceEvaluation, ByVal g As Goal, ByVal ratingLevel As RatingLevel)

        '    Dim session As ISession = NHibernateHelper.GetCurrentSession()

        '    Dim gr As New GoalRating(pe, g, ratingLevel)

        '    pe.GoalRatings.Add(gr)
        '    session.SaveOrUpdate(gr)
        '    session.Flush()

        'End Sub

        Public Sub SaveOrUpdateGoalRating(ByVal gr As GoalRating)

            Try

                Dim session As ISession = NHibernateHelper.GetCurrentSession()

                session.SaveOrUpdate(gr)
                session.Flush()

            Catch ex As Exception

                Throw New TechnicalException(String.Format("Error while trying to 'SaveOrUpdateGoalRating' - Details: {0}", _
                                                             ex.Message), ex.InnerException)

            End Try


        End Sub

    End Class

End Namespace


